/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.services.reports;

import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Provider;
import org.jfree.chart.JFreeChart;
import org.xptracker.domain.Domain;
import org.xptracker.domain.DomainUtil;
import org.xptracker.domain.TimeSegment;
import org.xptracker.domain.Timeline;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class ReportFactory {
  private static final ConcurrentMap<Timeline, JFreeChart> TIMELINE_CHARTS =
      new ConcurrentHashMap<Timeline, JFreeChart>();

  @SuppressWarnings({"UnusedDeclaration"})
  @Inject
  private Provider<TimeSegmentReport> reportProvider;

  @SuppressWarnings({"UnusedDeclaration"})
  @Inject
  private Provider<StoryHistory> historyProvider;

  @SuppressWarnings({"UnusedDeclaration"})
  @Inject
  private Domain domain;

  @Inject
  private static Injector injector;

  public <T> StoryHistory<T> makeStoryHistory(StoryHistory.StateRetriever<T> retriever, String storyId) {
    //noinspection unchecked
    StoryHistory<T> h = historyProvider.get();
    h.setStoryId(storyId);
    h.setRetriever(retriever);
    return h;
  }

  public TimeSegmentReport makeTimeSegmentReport(Timeline timeline, TimeSegment timeSegment) {
    TimeSegmentReport r = reportProvider.get();
    r.setTimeline(timeline);
    r.setSegment(timeSegment);
    return r;
  }

  public void loadTimelineCharts() {
    TIMELINE_CHARTS.clear();
    for(Timeline t: DomainUtil.fullyLoadItems(domain.listTimelines())) {
      TimeSegment segment = t.getCurrentSegment();
      if (segment != null) {
        TimeSegmentReport report = makeTimeSegmentReport(t, segment);
        ChartBuildingReportRecipient r = new ChartBuildingReportRecipient(segment);
        report.execute(r);
        TIMELINE_CHARTS.put(t, r.buildChart());
      }
    }
  }

  public JFreeChart getTimelineChart(Timeline t) {
    JFreeChart chart = t != null ? TIMELINE_CHARTS.get(t) : null;
    return chart == null ? null : chart;
  }

  /**
   * Convenience method for places in the code (i.e. custom JSP tags) that aren't managed by Guice.  Please
   * just put an @Inject annotation on whatever you want to use that needs a ReportFactory.
   * @return the report factory that is provided by the Guice injector
   */
  public static ReportFactory getInstance() {
    return injector.getInstance(ReportFactory.class);
  }
}
